import Vue from "vue";
import axios from "axios";

let config = {};

const setConfig = cfg => {
	config = cfg;
};

const getConfig = () => {
	return config;
};

// 获取项目动态全局配置
export const getServerConfig = () => {
	return axios({
		baseURL: "",
		method: "get",
		url: `/serverConfig.json`
	})
		.then(({ data }) => {
			// 自动注入项目配置
			if (data && typeof data === "object") {
				Vue.prototype.$config = data;
				// 设置全局配置
				setConfig(data);
			}
		})
		.catch(() => {
			throw "请在public文件夹下添加serverConfig.json配置文件";
		});
};

export { getConfig, setConfig };
